package com.dragee.modules.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.Dic;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessException;
import com.dragee.modules.work.entity.ShopClerkEntity;
import com.dragee.modules.work.entity.ShopEntity;
import com.dragee.modules.work.service.ShopClerkService;
import com.dragee.modules.work.service.ShopService;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 店员
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-05-23 09:57:35
 */
@RestController
@RequestMapping("work/shopclerk")
@Api(tags = "work - 店员")
public class ShopClerkController {
    @Autowired
    private ShopClerkService shopClerkService;
    @Autowired
    private ShopService shopService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("店员列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = shopClerkService.queryPage(params);
        return R.ok().put("data", page);
    }





    @GetMapping("availableClerkDic")
    @ApiOperation("可分配店员Dic")
    public R availableClerkDic (){
        ShopEntity shop = shopService.getShopByUserId(LoginUserUtil.getLoginUserId().toString());
        List<ShopClerkEntity> datas = shopClerkService.list(new QueryWrapper<ShopClerkEntity>()
                .eq("shopId", shop.getId()).eq("status", "在职"));
        List<Dic> dics = datas.stream().map(d -> new Dic(d.getName(), d.getId().toString())).collect(Collectors.toList());
        return R.ok().put("data", dics);
    }

    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("店员信息")
    public R info(@PathVariable("id") Long id){
		ShopClerkEntity shopClerk = shopClerkService.getById(id);
        return R.ok().put("data", shopClerk);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改店员")
    public R saveOrUpdate(@RequestBody ShopClerkEntity shopClerk){
        ShopEntity shop = shopService.getShopByUserId(LoginUserUtil.getLoginUserId().toString());
        if (shop == null) {
            throw new BusinessException("当前登录人没有店铺，不支持添加店员");
        }
        shopClerk.setShopId(shop.getId().toString());
        shopClerkService.saveOrUpdate(shopClerk);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("店员删除")
    public R delete( String id){
		shopClerkService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("店员批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> shopClerkService.removeById(Long.parseLong(id)));
        return R.ok();
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("/import")
    public R importData(@RequestParam("file") MultipartFile file) throws IOException {
        return shopClerkService.importData(file);
    }

    @ApiOperation(value = "数据导出")
    @GetMapping("/export")
    public void exportData(@RequestParam Map<String, Object> params, HttpServletResponse response ) throws Exception {
            shopClerkService.exportData(params, response);
    }

}
